	version 10.1
	#delimit;
	clear;
	pause on;
	set more off;
	  quietly log;
	  local logon = r(status);
	  if "`logon'" == "on" {; log close; };
	log using tables1&2, text replace;

		/************************************************************************/
		/* 	Author:		Olga Chyzh/Mark Nieman					  		*/
		/*	Date:		February 3, 2013			   		  		*/
		/*      File:		tax_analysis02.do					  		*/
		/*	Purpose:	Analysis for the Spatial Tax paper, after R&R at PSRM. will add oil, rivalry, and robustness	*/
			/*      Input File:		tax_data05.dta, spreg1.ado			*/
		/*      Output File:	tables1&2.log, table*		*/
		/************************************************************************/
		
	clear matrix;
	clear mata;
	*set mem 4g;
	set	matsize 11000;
	program drop _all;
	do spreg1.ado;
		use tax_data05.dta, clear;	
	
			
	sort ccode ccode1 ccode2 year;
	
	egen ccode_year=group(ccode year);
	egen ccode1_year=group(ccode1 year);
	egen ccode2_year=group(ccode2 year);
	gen t=year if ccode!=.;

	/*Row Standardizing the spatial variables*/
	egen tot_sim=total(reg_sim), by(ccode1_year);
	egen tot_cont=total(bi_contig), by(ccode1_year);
	egen tot_trade=total(dyad_trade), by(ccode1_year);
	egen tot_rival=total(diehl_riv), by(ccode1_year);
	egen tot_econ=total(dyadic_econ), by(ccode1_year);
	
	
	
	gen regsim_st=reg_sim/tot_sim;
	gen cont_st=bi_contig/tot_cont;
	gen trade_st=dyad_trade/tot_trade;
	gen riv_st=diehl_riv/tot_rival;
	gen io_ec_st=dyadic_econ/tot_econ;
	

	recode regsim_st (.=0) if reg_sim!=.;
	recode cont_st (.=0) if bi_contig!=.;
	recode trade_st (.=0) if dyad_trade!=.;
	recode riv_st (.=0) if diehl_riv!=.;
	recode io_ec_st (.=0) if dyadic_econ!=.;
	

	
	recode cont_st trade_st regsim_st cont_cwar cont_onset riv_st io_ec_st (.=0); 
	tabulate ccode, gen(country);
	
	
	
	/*Table 1. Summary Statistics*/
	/*Run a regression first to get the sample*/
	reg taxratio open cwar debt inflation  agri oda federal log_gdpenl ethfrac relfrac nwstate instab oil1  
	pres_dem monarch milit party_aut pers_aut other oilprice2009barrel lag_taxratio;
	
	sutex taxratio open cwar debt inflation  agri oda federal log_gdpenl ethfrac relfrac nwstate instab oil1  
	pres_dem monarch milit party_aut pers_aut other oilprice2009barrel lag_taxratio year if e(sample), minmax label file(tables/sum_part1) replace;
	
	sutex cont_st riv_st regsim_st trade_st io_ec_st, minmax labels file(tables/sum_part2) replace;
	

	
	
	/*One-by-one spatial lag, fixed effects*/
	spreg1  taxratio open cwar debt inflation  agri oda federal log_gdpenl ethfrac relfrac nwstate instab oil1  
	pres_dem monarch milit party_aut pers_aut other oilprice2009barrel lag_taxratio country1-country37 country39-country104 country106-country116 country118-country137, 
	 spatialvars(cont_st) id(ccode_year) rowid(ccode1_year) colid(ccode2_year) ; 
	 estimates store sp_tax_cont_fe;
	 
	 spreg1  taxratio open cwar debt inflation  agri oda federal log_gdpenl ethfrac relfrac nwstate instab oil1  
	pres_dem monarch milit party_aut pers_aut other oilprice2009barrel lag_taxratio country1-country37 country39-country104 country106-country116 country118-country137, 
	 spatialvars(riv_st) id(ccode_year) rowid(ccode1_year) colid(ccode2_year) ; 
	 estimates store sp_tax_riv_fe;
	 
	 spreg1  taxratio open cwar debt inflation  agri oda federal log_gdpenl ethfrac relfrac nwstate instab oil1  
	pres_dem monarch milit party_aut pers_aut other oilprice2009barrel lag_taxratio country1-country37 country39-country104 country106-country116 country118-country137, 
	 spatialvars(regsim_st) id(ccode_year) rowid(ccode1_year) colid(ccode2_year) ; 
	 estimates store sp_tax_reg_fe;
	 
	  
	 spreg1  taxratio open cwar debt inflation  agri oda federal log_gdpenl ethfrac relfrac nwstate instab oil1  
	pres_dem monarch milit party_aut pers_aut other oilprice2009barrel lag_taxratio country1-country37 country39-country104 country106-country116 country118-country137, 
	 spatialvars(trade_st) id(ccode_year) rowid(ccode1_year) colid(ccode2_year) ; 
	 estimates store sp_tax_trade_fe;
	 
	  
	 spreg1  taxratio open cwar debt inflation  agri oda federal log_gdpenl ethfrac relfrac nwstate instab oil1  
	pres_dem monarch milit party_aut pers_aut other oilprice2009barrel lag_taxratio country1-country37 country39-country104 country106-country116 country118-country137, 
	 spatialvars(io_ec_st) id(ccode_year) rowid(ccode1_year) colid(ccode2_year) ; 
	 estimates store sp_tax_io_fe;
	
	/*Model 6*/
	spreg1  taxratio open cwar debt inflation  agri oda federal log_gdpenl ethfrac relfrac nwstate instab oil1  
	pres_dem monarch milit party_aut pers_aut other oilprice2009barrel lag_taxratio country1-country37 country39-country104 country106-country116 country118-country137, 
	 spatialvars(cont_st riv_st regsim_st trade_st io_ec_st) id(ccode_year) rowid(ccode1_year) colid(ccode2_year) ; 

	estimates store sp_tax_fe;
	 
matrix b=e(b);
svmat b, names(b_fe);

#delimit;
matrix V=e(V);
svmat V, names(V_fe);

/*Model 7*/	
	spreg1  taxratio open cwar debt inflation  agri oda federal log_gdpenl ethfrac relfrac nwstate instab oil1  
	pres_dem monarch milit party_aut pers_aut other oilprice2009barrel lag_taxratio, 
	 spatialvars(cont_st riv_st regsim_st trade_st io_ec_st) id(ccode_year) rowid(ccode1_year) colid(ccode2_year) ; 
	estimates store sp_tax;
	 
matrix b=e(b);
svmat b, names(b);


matrix V=e(V);
svmat V, names(V);
save tax_estimates.dta, replace;

/*Table 2. Multi-parametric Spatio-Temporal Autoregression on Tax Ratio, 1960-1999 */
estout sp_tax_cont_fe sp_tax_riv_fe sp_tax_trade_fe sp_tax_reg_fe sp_tax_io_fe  sp_tax_fe sp_tax using "tables/table1", replace style(tex)
	stats(ll N )
	cells(b(fmt(3) star) se(par))
	modelwidth(8)
	starlevels(* 0.1 ** 0.05 *** 0.01) legend
	varwidth(30)
	order(lag_taxratio open cwar debt inflation  agri oda federal log_gdpenl ethfrac relfrac nwstate instab oil1  
	pres_dem monarch milit party_aut pers_aut other oilprice2009barrel)
	collabels(, none)
	varlabels(lag_taxratio 	"Tax Ratio (t-1)"
	  open		"Trade Openness"
	  cwar		"Civil War"
	  debt		"Debt"
	  inflation		"Inflation"
	  agri		"Agricultural Dev."
	  oda		"Foreign Aid"
	  federal 	"Federalism"
	  log_gdpenl	"GDP/cap."
	  ethfrac     "Ethnic Fract."
	  relfrac     "Relig. Fract."
	  nwstate     "New State"
	  instab      "Instability"
	  oil1       "Oil"
      pres_dem    "Presidential Dem."
	  monarch     "Monarchy"
	  milit       "Military Regime"
	  party_aut   "Single-party Regime"
	  pers_aut    "Personalist Regime"
	  other       "Other Regime"
	  oilprice2009barrel "Global Oil Price");

	 
log close;	 

